package br.upe.dsc.caeto.core.cacheunit;

import br.upe.dsc.caeto.simulation.SimulationContext;
import java.util.Iterator;
import br.upe.dsc.caeto.core.IRegister;
import br.upe.dsc.caeto.core.IRepository;
import br.upe.dsc.caeto.core.InvalidElementException;
import br.upe.dsc.caeto.core.ElementNotFoundException;
import br.upe.dsc.caeto.core.application.Application;

public class CacheUnitRegister implements IRegister<CacheUnit>,
                                         Iterable<CacheUnit> {
	IRepository<CacheUnit> repository;
	SimulationContext context = new SimulationContext();
	
	public CacheUnitRegister(IRepository repository) {
		this.repository = repository;
	}

	public void add(CacheUnit cache) throws InvalidElementException {
		if (!context.validateCacheUnit(cache)) {
			throw new InvalidElementException("Configuracao da cache " +
					cache.getName() + " invalida.");
		}
		this.repository.insert(cache);
	}
	
	public void remove(CacheUnit cache) throws ElementNotFoundException {
		this.repository.remove(cache);
	}
	
	public void update(CacheUnit cache) throws InvalidElementException {
		if (!context.validateCacheUnit(cache)) {
			throw new InvalidElementException("Configuracao da cache " +
					cache.getName() + " invalida.");
		}
		this.repository.update(cache);
	}
	
	public CacheUnit search(String name) {
		return this.repository.search(name);
	}
	
	public boolean isElement(String name) {
		return this.repository.isElement(name);
	}
	
	public void empty() {
		this.repository.empty();
	}
	
	public Iterator<CacheUnit> iterator() {
		return this.repository.iterator();
	}
	
	public Iterable<CacheUnit> getElements() {
		return this.repository.getElements();
	}
}
